package aishua.com.mytestapplist.diyiview;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Scroller;

import aishua.com.mytestapplist.utils.LogUtils;

/**
 * Created by aishua on 2017/3/31.
 */

public class CustomView extends ViewGroup{

        private static final String TAG = "CustomView--";
        public CustomView(Context context, AttributeSet attrs) {
            super(context, attrs);
            LogUtils.v(TAG + "CustomView()");
           setClickable(true);

        }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        LogUtils.v(TAG+"onMeasure()");
//        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//         View view =getChildAt(0);
//        view.measure(widthMeasureSpec,heightMeasureSpec);
//        View  view1 =getChildAt(1);
//        view1.measure(widthMeasureSpec,heightMeasureSpec);

        int mode_w = MeasureSpec.getMode(widthMeasureSpec);
        int size_w = MeasureSpec.getSize(widthMeasureSpec);
        int mode_h = MeasureSpec.getMode(heightMeasureSpec);
        int size_h = MeasureSpec.getSize(heightMeasureSpec);
        LogUtils.v(TAG + "size_w-->" + size_w + ",size_h--->" + size_h);
        measureChildren(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(size_w,size_h);

    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {

        LogUtils.v(TAG+"onLayout()");
        LogUtils.v(TAG + "changed-->" + changed + ",left--->" + l + ",top-->" + t + ",right-->" + r + ",bottom--->" + b);

//        super.onLayout(changed, l, t, r, b);
        int count = getChildCount();
        LogUtils.v(TAG+"count"+count);
//        int h = 0;
        for (int i=0;i<count;i++){
            View view = getChildAt(i);
            int h=  view.getMeasuredHeight();
            int w = view.getMeasuredWidth();
//            view.layout(0, h, view.getMeasuredWidth(),h +  view.getMeasuredHeight());
//            h += view.getMeasuredHeight();
            view.layout(0,h*i,w,h*(i+1));
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        int e = event.getAction();
        switch (e) {
            case MotionEvent.ACTION_DOWN:
               LogUtils.v(TAG + "onTouchEvent()--MotionEvent.ACTION_DOWN---"+super.onTouchEvent(event));
//                return  true;
//                return  false;
                break;
            case MotionEvent.ACTION_MOVE:
               LogUtils.v(TAG + "onTouchEvent()--MotionEvent.ACTION_MOVE");
//                return false;
                break;
            case MotionEvent.ACTION_UP:
               LogUtils.v(TAG + "onTouchEvent()--MotionEvent.ACTION_UP");
               break;
//                return true;
        }
        return super.onTouchEvent(event);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {

        int e = ev.getAction();
        switch (e) {
            case MotionEvent.ACTION_DOWN:
               LogUtils.v(TAG + "dispatchTouchEvent()--MotionEvent.ACTION_DOWN");
                return  false;
//                break;
//                return  false;
            case MotionEvent.ACTION_MOVE:
               LogUtils.v(TAG + "dispatchTouchEvent()--MotionEvent.ACTION_MOVE");
//                return  false;
                break;
            case MotionEvent.ACTION_UP:
               LogUtils.v(TAG + "dispatchTouchEvent()--MotionEvent.ACTION_UP");
//                 return false;
                break;
        }
        return super.dispatchTouchEvent(ev);
//        return true;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {

        int e = ev.getAction();
        switch (e) {
            case MotionEvent.ACTION_DOWN:
               LogUtils.v(TAG + "onInterceptTouchEvent()--MotionEvent.ACTION_DOWN");
//                return  false;
                break;
//                return  true;
            case MotionEvent.ACTION_MOVE:
               LogUtils.v(TAG + "onInterceptTouchEvent()--MotionEvent.ACTION_MOVE");
//                return  false;
                break;
            case MotionEvent.ACTION_UP:
               LogUtils.v(TAG + "onInterceptTouchEvent()--MotionEvent.ACTION_UP");
//                 return false;
                break;
        }
        return super.onInterceptTouchEvent(ev);
//        return true;
    }

    @Override
    public void requestDisallowInterceptTouchEvent(boolean disallowIntercept){
       LogUtils.v(TAG+"requestDisallowInterceptTouchEvent()");

        super.requestDisallowInterceptTouchEvent(disallowIntercept);
    }


}
